# -*- coding:utf-8 -*-

"""
=============================
this is a feibot product,
all rights reserved.
www.feibot.com
@author: Gu Tianyuan
@email: g@feibot.com
@time: 2023/5/12:9:31
@product: PyCharm
=============================
"""


def crcCheck():
    return True


def getHex(i):
    if i in '1234567890':
        return int(i)
    if i in 'aA':
        return 10
    if i in 'bB':
        return 11
    if i in 'cC':
        return 12
    if i in 'dD':
        return 13
    if i in 'eE':
        return 14
    if i in 'fF':
        return 15


def getHexStr(t):
    resStr = ''
    for num in range(0, len(t)):
        if t[num] < 16:
            resStr += "0" + hex(t[num])[2:]
        else:
            resStr += hex(t[num])[2:]
    return resStr.upper()


READER_RX_BUF_SIZE = 1024


class Decode:
    def __init__(self):
        self.buf = {}
        self.tail = 0
        self.content = ''
        self.rawData = []

    def readerDataIn(self):
        with open("./rawDataFromReader/原始数据.txt", "r") as f:
            self.content = f.read()
        raw = self.content.split(" ")
        for word in raw:
            if len(word) > 0:
                self.rawData.append(getHex(word[0]) * 16 + getHex(word[1]))
        print(self.rawData)

    def decode(self, ch):
        i = 0
        j = 0
        data_len = 0
        success = 0
        cmd_id = 0
        data = {}
        data_length = 0
        self.buf[self.tail] = ch

        for i in range(0, self.tail):
            if self.buf[i] == 0xff:
                if self.tail >= i + 1:
                    data_len = self.buf[i + 1]
                    if self.tail >= i + data_len + 6:
                        if crcCheck():
                            cmd_id = self.buf[i + 2]
                            for j in range(0, data_len):
                                data[j] = self.buf[j + i + 5]
                            data_length = data_len
                            success = 1
        if success:
            self.tail = 0
            return 1, cmd_id, data, data_length
        else:
            if self.tail + 1 < READER_RX_BUF_SIZE:
                self.tail = self.tail + 1
            else:
                self.tail = 0
            return 0, cmd_id, data, data_length


if __name__ == '__main__':
    d = Decode()
    d.readerDataIn()
    res = (0, 0, 0, 0)
    successCounter = 0
    decoded = {}
    for ch in d.rawData:
        res = d.decode(ch)
        success = res[0]
        cmd_id = res[1]
        data = res[2]
        data_length = res[3]
        epcRaw = {}
        if success == 1 and cmd_id == 170 and data[0] == 0 and data[1] == 0x16:
            epcLen = data_length - 13
            for i in range(0, epcLen):
                epcRaw[i] = data[11 + i]
            epc = getHexStr(epcRaw)
            print(epc)
            if epc in decoded:
                decoded[epc] += 1
            else:
                decoded[epc] = 1
            successCounter += 1
    print(successCounter)
    for k,v in decoded.items():
        print(k,v)
